home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / fsinstall / RCS / diskHeader.c,v < prev    next >
Text File  |  1990-01-31  |  7KB  |  262 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     90.01.31.13.54.05;  author shirriff;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @File as of 1-31-90, before replaced by symbolic link.
  17. @
  18.  
  19.  
  20.  
  21. 1.1
  22. log
  23. @Initial revision
  24. @
  25. text
  26. @/* 
  27.  * diskHeader.c --
  28.  *
  29.  *    Routines to read in the disk header information.
  30.  *
  31.  * Copyright (C) 1987 Regents of the University of California
  32.  * All rights reserved.
  33.  * Permission to use, copy, modify, and distribute this
  34.  * software and its documentation for any purpose and without
  35.  * fee is hereby granted, provided that the above copyright
  36.  * notice appear in all copies.  The University of California
  37.  * makes no representations about the suitability of this
  38.  * software for any purpose.  It is provided "as is" without
  39.  * express or implied warranty.
  40.  */
  41.  
  42. #ifndef lint
  43. static char rcsid[] = "$Header: /sprite/src/lib/c/disk/RCS/diskHeader.c,v 1.7 89/09/25 12:32:34 jhh Exp $ SPRITE (Berkeley)";
  44. #endif not lint
  45.  
  46. #include "diskUtils.h"
  47. #include <string.h>
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50.  
  51. /*
  52.  * BOOT_SECTOR        Where the boot sectors start on disk.
  53.  */
  54. #define    BOOT_SECTOR        1
  55. #define NUM_BOOT_SECTORS    15
  56.  
  57. /*
  58.  *----------------------------------------------------------------------
  59.  *
  60.  * Disk_ReadDiskInfo--
  61.  *
  62.  *    Read the first sector and determine the basic disk info.
  63.  *
  64.  * Results:
  65.  *    A pointer to Disk_Info for the disk if could read it, 0
  66.  *    otherwise.
  67.  *
  68.  * Side effects:
  69.  *    Memory allocation.
  70.  *
  71.  *----------------------------------------------------------------------
  72.  */
  73. Disk_Info *
  74. Disk_ReadDiskInfo(fileID, partition)
  75.     int fileID;            /* Handle on raw disk */
  76.     int partition;        /* Index of the partition to format */
  77. {
  78.     Sun_DiskLabel    *sunLabelPtr;
  79.     Disk_Info        *diskInfoPtr;
  80.     char         buffer[DEV_BYTES_PER_SECTOR * FSDM_NUM_DOMAIN_SECTORS];
  81.     Fsdm_DomainHeader    *domainHeaderPtr = (Fsdm_DomainHeader *) buffer;
  82.     int            i;
  83.  
  84.     diskInfoPtr = (Disk_Info *)malloc(sizeof(Disk_Info));
  85.  
  86.     sunLabelPtr = Disk_ReadSunLabel(fileID);
  87.     if (sunLabelPtr != (Sun_DiskLabel *)0) {
  88.     Sun_DiskMap *sunMapPtr;
  89.     /*
  90.      * First sector looks like a sun label.
  91.      */
  92.     sunMapPtr = &sunLabelPtr->map[partition];
  93.     (void)strcpy(diskInfoPtr->asciiLabel, sunLabelPtr->asciiLabel);
  94.     diskInfoPtr->bootSector = BOOT_SECTOR;
  95.     diskInfoPtr->numDomainSectors = FSDM_NUM_DOMAIN_SECTORS;
  96.     diskInfoPtr->firstCylinder = sunMapPtr->cylinder;
  97.     diskInfoPtr->numCylinders = sunMapPtr->numBlocks /
  98.         (sunLabelPtr->numHeads * sunLabelPtr->numSectors);
  99.     diskInfoPtr->numHeads = sunLabelPtr->numHeads;
  100.     diskInfoPtr->numSectors = sunLabelPtr->numSectors;
  101.     for (i = BOOT_SECTOR + 1; 
  102.          i < FSDM_MAX_BOOT_SECTORS + 3; 
  103.          i+= FSDM_BOOT_SECTOR_INC) {
  104.         if (Disk_SectorRead(fileID, i, FSDM_NUM_DOMAIN_SECTORS, buffer) < 0) {
  105.         fprintf(stderr, "Can't read sector %d.\n", i);
  106.         return((Disk_Info *)0);
  107.         }
  108.         if (domainHeaderPtr->magic == FSDM_DOMAIN_MAGIC) {
  109.         diskInfoPtr->summarySector = i - 1;
  110.         diskInfoPtr->domainSector = i;
  111.         diskInfoPtr->numBootSectors = diskInfoPtr->summarySector - 1;
  112.         break;
  113.         }
  114.     }
  115.     if (i >= FSDM_MAX_BOOT_SECTORS + 3) {
  116.         diskInfoPtr->summarySector = -1;
  117.         diskInfoPtr->domainSector = -1;
  118.         diskInfoPtr->numBootSectors = -1;
  119.     }
  120.     } else {
  121.     Fsdm_DiskHeader *diskHeaderPtr;
  122.     Fsdm_DiskPartition *mapPtr;
  123.  
  124.     /*
  125.      * Not a sun label, try a sprite label.
  126.      */
  127.     diskHeaderPtr = Disk_ReadDiskHeader(fileID);
  128.     if (diskHeaderPtr == (Fsdm_DiskHeader *)0) {
  129.         fprintf(stderr, "Neither Sun nor Sprite label\n");
  130.         return((Disk_Info *)0);
  131.     }
  132.     mapPtr = &diskHeaderPtr->map[partition];
  133.     (void)strcpy(diskInfoPtr->asciiLabel, diskHeaderPtr->asciiLabel);
  134.     diskInfoPtr->bootSector = diskHeaderPtr->bootSector;
  135.     diskInfoPtr->numBootSectors = diskHeaderPtr->numBootSectors;
  136.     diskInfoPtr->summarySector = diskHeaderPtr->summarySector;
  137.     diskInfoPtr->domainSector = diskHeaderPtr->domainSector;
  138.     diskInfoPtr->numDomainSectors = diskHeaderPtr->numDomainSectors;
  139.     diskInfoPtr->firstCylinder = mapPtr->firstCylinder;
  140.     diskInfoPtr->numCylinders = mapPtr->numCylinders;
  141.     diskInfoPtr->numHeads = diskHeaderPtr->numHeads;
  142.     diskInfoPtr->numSectors = diskHeaderPtr->numSectors;
  143.     }
  144.     return(diskInfoPtr);
  145. }
  146.  
  147.  
  148.  
  149. /*
  150.  *----------------------------------------------------------------------
  151.  *
  152.  * Disk_ReadSunLabel --
  153.  *
  154.  *    Read the first sector of a disk partition and see if its
  155.  *    a sun label.  If so, return a pointer to a Sun_DiskLabel.
  156.  *
  157.  * Results:
  158.  *    A pointer to the super block data if could read it, 0 otherwise.
  159.  *
  160.  * Side effects:
  161.  *    Memory allocation.
  162.  *
  163.  *----------------------------------------------------------------------
  164.  */
  165. Sun_DiskLabel *
  166. Disk_ReadSunLabel(fileID)
  167.     int fileID;    /* Handle on raw disk */
  168. {
  169.     Address        buffer;
  170.     Sun_DiskLabel    *sunLabelPtr;
  171.  
  172.     buffer = (Address)malloc(DEV_BYTES_PER_SECTOR);
  173.  
  174.     if (Disk_SectorRead(fileID, 0, 1, buffer) < 0) {
  175.     return((Sun_DiskLabel *)0);
  176.     } else {
  177.     sunLabelPtr = (Sun_DiskLabel *)buffer;
  178.     if (sunLabelPtr->magic != SUN_DISK_MAGIC) {
  179.         return((Sun_DiskLabel *)0);
  180.     } else {
  181.         return(sunLabelPtr);
  182.     }
  183.     }
  184. }
  185.  
  186. /*
  187.  *----------------------------------------------------------------------
  188.  *
  189.  * Disk_ReadDiskHeader --
  190.  *    Read the super block and return a pointer to it.
  191.  *
  192.  * Results:
  193.  *    A pointer to the super block data if could read it, 0 otherwise.
  194.  *
  195.  * Side effects:
  196.  *    Memory allocation.
  197.  *
  198.  *----------------------------------------------------------------------
  199.  */
  200. Fsdm_DiskHeader *
  201. Disk_ReadDiskHeader(openFileID)
  202.     int openFileID;    /* Handle on raw disk */
  203. {
  204.     Address        buffer;
  205.     Fsdm_DiskHeader    *diskHeaderPtr;
  206.  
  207.     buffer = (Address) malloc(DEV_BYTES_PER_SECTOR);
  208.  
  209.     if (Disk_SectorRead(openFileID, 0, 1, buffer) < 0) {
  210.     return((Fsdm_DiskHeader *)0);
  211.     } else {
  212.     diskHeaderPtr = (Fsdm_DiskHeader *)buffer;
  213.     if (diskHeaderPtr->magic != FSDM_DISK_MAGIC) {
  214.         return((Fsdm_DiskHeader *)0);
  215.     } else {
  216.         return(diskHeaderPtr);
  217.     }
  218.     }
  219. }
  220.  
  221. /*
  222.  *----------------------------------------------------------------------
  223.  *
  224.  * Disk_ReadDomainHeader --
  225.  *    Read the domain header and return a pointer to it.
  226.  *
  227.  * Results:
  228.  *    A pointer to the domain header if could read it, NULL otherwise.
  229.  *
  230.  * Side effects:
  231.  *    Memory allocation.
  232.  *
  233.  *----------------------------------------------------------------------
  234.  */
  235. Fsdm_DomainHeader *
  236. Disk_ReadDomainHeader(fileID, diskInfoPtr)
  237.     int fileID;            /* Stream to raw disk */
  238.     Disk_Info *diskInfoPtr;    /* Basic disk information */
  239. {
  240.     Fsdm_DomainHeader    *headerPtr;
  241.  
  242.     headerPtr = (Fsdm_DomainHeader *)malloc(diskInfoPtr->numDomainSectors *
  243.                      DEV_BYTES_PER_SECTOR);
  244.  
  245.     if (Disk_SectorRead(fileID, diskInfoPtr->domainSector,
  246.                     diskInfoPtr->numDomainSectors,
  247.                     (Address)headerPtr) < 0) {
  248.     return((Fsdm_DomainHeader *)0);
  249.     } else {
  250.     if (headerPtr->magic != FSDM_DOMAIN_MAGIC) {
  251.         fprintf(stderr, "Disk_ReadDomainHeader, bad magic <%x>\n",
  252.             headerPtr->magic);
  253.         free((Address)headerPtr);
  254.         return((Fsdm_DomainHeader *)0);
  255.     } else {
  256.         return(headerPtr);
  257.     }
  258.     }
  259. }
  260.  
  261. @
  262.